package com.javacoo.xservice.base.support.event;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.task.TaskExecutor;

/**
 * 异步事件处理
 * <p>说明:</p>
 * <li>为了实现异步事件处理，这里需要重新实现SimpleApplicationEventMulticaste</li>
 * @author DuanYong
 * @since 2017年7月3日上午9:56:57
 */
@Configuration(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
public class AsyncApplicationEventMulticaster extends SimpleApplicationEventMulticaster {  
    
	private TaskExecutor taskExecutor = new TaskExecutor() {  
		ThreadFactory threadFactory = Executors.defaultThreadFactory();
        ExecutorService executorService = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors(),
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),threadFactory);
        @Override
        public void execute(Runnable task) {  
        	executorService.execute(task);  
        }  
    };  
    @Override
    protected TaskExecutor getTaskExecutor() {  
        return this.taskExecutor;  
    }  

}
